﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient ;

namespace WG计划管理工具
{
    public partial class frmPermissionManage : Form
    {
        SqlTools.SqlTools sqt;

        //查询系统中用户信息表
        string strSelectUserID = "select yh001 用户编号,yh002 用户名称 from yh_table";
        //查询指定用户权限
        string strSelectQx = "select 权限 from wgqx where 用户编号='@用户编号'";
        //删除指定用户全部权限
        string strDeleteQx = "delete from wgqx where 用户编号='@用户编号'";
        //插入指定用户权限
        string strInsertQx = "insert into wgqx (用户编号,权限) values ('@用户编号','@权限')";
        public frmPermissionManage()
        {
            InitializeComponent();
        }

        private void frmPermissionManage_Load(object sender, EventArgs e)
        {
            sqt = new SqlTools.SqlTools(WG计划管理工具.Properties.Settings.Default.ConnectionString);
            iniComboBox();
        }

        private void iniComboBox()
        {
            DataTable tmpdt = sqt.GetDataTable(strSelectUserID);
            cmbUserID.Items.Clear();
            cmbUserID.Tag = null;
            if (tmpdt.Rows.Count > 0)
            {
                string[] strUserNames = new string[tmpdt.Rows.Count];
                for (int i = 0; i < tmpdt.Rows.Count; i++)
                {
                    cmbUserID.Items.Add(tmpdt.Rows[i]["用户编号"]);
                    strUserNames[i] = GetString(tmpdt.Rows[i]["用户名称"]);
                }
                cmbUserID.Tag = strUserNames;
                cmbUserID.SelectedIndex = 0;
            }
        }

        private string GetString(object tmpobj)
        {
            try
            {
                return tmpobj.ToString();
            }
            catch
            {
                return string.Empty;
            }
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            if (cmbUserID.Tag != null)
            {
                string[] strUserNames = (string[])cmbUserID.Tag;
                if (strUserNames.Length > 0)
                {
                    for (int i = 0; i < strUserNames.Length; i++)
                    {
                        if (strUserNames[i].StartsWith(txtUserName.Text.Trim()))
                        {
                            cmbUserID.SelectedIndex = i;
                            return;
                        }
                    }
                }
            }
            if (cmbUserID.Items.Count > 0)
            {
                for (int i = 0; i < cmbUserID.Items.Count; i++)
                {
                    if (cmbUserID.Items[i].ToString().StartsWith(txtUserName.Text.Trim()))
                    {
                        cmbUserID.SelectedIndex = i;
                        return;
                    }
                }
            }
        }

        private void cmbUserID_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cmbUserID.Tag != null)
            {
                string[] strUserNames = (string[])cmbUserID.Tag;
                txtUserName.Text = strUserNames[cmbUserID.SelectedIndex];
            }
            if (cmbUserID.Text.Trim().Length > 0)
            {
                foreach (Control c in groupBox1 .Controls )
                {
                    if (c.GetType().Name =="CheckBox")
                    {
                        ((CheckBox)c).Checked = false;
                    }
                }
                foreach (Control c in groupBox2.Controls)
                {
                    if (c.GetType().Name == "CheckBox")
                    {
                        ((CheckBox)c).Checked = false;
                    }
                }
                foreach (Control c in groupBox3.Controls)
                {
                    if (c.GetType().Name == "CheckBox")
                    {
                        ((CheckBox)c).Checked = false;
                    }
                }
                foreach (Control c in groupBox4.Controls)
                {
                    if (c.GetType().Name == "CheckBox")
                    {
                        ((CheckBox)c).Checked = false;
                    }
                }
                foreach (Control c in groupBox5.Controls)
                {
                    if (c.GetType().Name == "CheckBox")
                    {
                        ((CheckBox)c).Checked = false;
                    }
                }
                DataTable tmpdt = sqt.GetDataTable(strSelectQx.Replace("@用户编号", cmbUserID.Text));
                for (int i = 0; i < tmpdt.Rows.Count; i++)
                {
                    switch (tmpdt.Rows[i]["权限"].ToString())
                    {
                        case "sq1":
                            chksq1.Checked = true ;
                            break;
                        case "sq2":
                            chksq2.Checked = true;
                            break;
                        case "sq3":
                            chksq3.Checked = true;
                            break;
                        case "sq4":
                            chksq4.Checked = true;
                            break;
                        case "sq5":
                            chksq5.Checked = true;
                            break;
                        case "sq6":
                            chksq6.Checked = true;
                            break;
                        case "jh1":
                            chkjh1.Checked = true;
                            break;
                        case "jh2":
                            chkjh2.Checked = true;
                            break;
                        case "jh3":
                            chkjh3.Checked = true;
                            break;
                        case "jh4":
                            chkjh4.Checked = true;
                            break;
                        case "jh5":
                            chkjh5.Checked = true;
                            break;
                        case "jh6":
                            chkjh6.Checked = true;
                            break;
                        case "jh7":
                            chkjh7.Checked = true;
                            break;
                        case "rk1":
                            chkrk1.Checked = true;
                            break;
                        case "rk2":
                            chkrk2.Checked = true;
                            break;
                        case "rk3":
                            chkrk3.Checked = true;
                            break;
                        case "rk4":
                            chkrk4.Checked = true;
                            break;
                        case "rk5":
                            chkrk5.Checked = true;
                            break;
                        case "rk6":
                            chkrk6.Checked = true;
                            break;
                        case "rk7":
                            chkrk7.Checked = true;
                            break;
                        case "ck1":
                            chkck1.Checked = true;
                            break;
                        case "ck2":
                            chkck2.Checked = true;
                            break;
                        case "ck3":
                            chkck3.Checked = true;
                            break;
                        case "ck4":
                            chkck4.Checked = true;
                            break;
                        case "ck5":
                            chkck5.Checked = true;
                            break;
                        case "ck6":
                            chkck6.Checked = true;
                            break;
                        case "set1":
                            chkset1.Checked = true;
                            break;
                    }
                }
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (cmbUserID.Text.Trim().Length > 0)
            {
                sqt.DoCmdExecuteNonQuery(strDeleteQx.Replace("@用户编号", cmbUserID.Text));
                string strTmpInsertCommand=strInsertQx .Replace ("@用户编号",cmbUserID .Text );
                if (chksq1.Checked)
                {
                    sqt.DoCmdExecuteNonQuery(strTmpInsertCommand.Replace("@权限", "sq1"));
                }
                if (chksq2.Checked)
                {
                    sqt.DoCmdExecuteNonQuery(strTmpInsertCommand.Replace("@权限", "sq2"));
                }
                if (chksq3.Checked)
                {
                    sqt.DoCmdExecuteNonQuery(strTmpInsertCommand.Replace("@权限", "sq3"));
                }
                if (chksq4.Checked)
                {
                    sqt.DoCmdExecuteNonQuery(strTmpInsertCommand.Replace("@权限", "sq4"));
                }
                if (chksq5.Checked)
                {
                    sqt.DoCmdExecuteNonQuery(strTmpInsertCommand.Replace("@权限", "sq5"));
                }
                if (chksq6.Checked)
                {
                    sqt.DoCmdExecuteNonQuery(strTmpInsertCommand.Replace("@权限", "sq6"));
                }
                if (chkjh1.Checked)
                {
                    sqt.DoCmdExecuteNonQuery(strTmpInsertCommand.Replace("@权限", "jh1"));
                }
                if (chkjh2.Checked)
                {
                    sqt.DoCmdExecuteNonQuery(strTmpInsertCommand.Replace("@权限", "jh2"));
                }
                if (chkjh3.Checked)
                {
                    sqt.DoCmdExecuteNonQuery(strTmpInsertCommand.Replace("@权限", "jh3"));
                }
                if (chkjh4.Checked)
                {
                    sqt.DoCmdExecuteNonQuery(strTmpInsertCommand.Replace("@权限", "jh4"));
                }
                if (chkjh5.Checked)
                {
                    sqt.DoCmdExecuteNonQuery(strTmpInsertCommand.Replace("@权限", "jh5"));
                }
                if (chkjh6.Checked)
                {
                    sqt.DoCmdExecuteNonQuery(strTmpInsertCommand.Replace("@权限", "jh6"));
                }
                if (chkjh7.Checked)
                {
                    sqt.DoCmdExecuteNonQuery(strTmpInsertCommand.Replace("@权限", "jh7"));
                }
                if (chkrk1.Checked)
                {
                    sqt.DoCmdExecuteNonQuery(strTmpInsertCommand.Replace("@权限", "rk1"));
                }
                if (chkrk2.Checked)
                {
                    sqt.DoCmdExecuteNonQuery(strTmpInsertCommand.Replace("@权限", "rk2"));
                }
                if (chkrk3.Checked)
                {
                    sqt.DoCmdExecuteNonQuery(strTmpInsertCommand.Replace("@权限", "rk3"));
                }
                if (chkrk4.Checked)
                {
                    sqt.DoCmdExecuteNonQuery(strTmpInsertCommand.Replace("@权限", "rk4"));
                }
                if (chkrk5.Checked)
                {
                    sqt.DoCmdExecuteNonQuery(strTmpInsertCommand.Replace("@权限", "rk5"));
                }
                if (chkrk6.Checked)
                {
                    sqt.DoCmdExecuteNonQuery(strTmpInsertCommand.Replace("@权限", "rk6"));
                }
                if (chkrk7.Checked)
                {
                    sqt.DoCmdExecuteNonQuery(strTmpInsertCommand.Replace("@权限", "rk7"));
                }
                if (chkck1.Checked)
                {
                    sqt.DoCmdExecuteNonQuery(strTmpInsertCommand.Replace("@权限", "ck1"));
                }
                if (chkck2.Checked)
                {
                    sqt.DoCmdExecuteNonQuery(strTmpInsertCommand.Replace("@权限", "ck2"));
                }
                if (chkck3.Checked)
                {
                    sqt.DoCmdExecuteNonQuery(strTmpInsertCommand.Replace("@权限", "ck3"));
                }
                if (chkck4.Checked)
                {
                    sqt.DoCmdExecuteNonQuery(strTmpInsertCommand.Replace("@权限", "ck4"));
                }
                if (chkck5.Checked)
                {
                    sqt.DoCmdExecuteNonQuery(strTmpInsertCommand.Replace("@权限", "ck5"));
                }
                if (chkck6.Checked)
                {
                    sqt.DoCmdExecuteNonQuery(strTmpInsertCommand.Replace("@权限", "ck6"));
                }
                if (chkset1.Checked)
                {
                    sqt.DoCmdExecuteNonQuery(strTmpInsertCommand.Replace("@权限", "set1"));
                }
                MessageBox.Show("保存成功！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
                MessageBox.Show("无有效用户！操作将停止", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
        }

        private void btnSelectAll1_Click(object sender, EventArgs e)
        {
            foreach (Control c in groupBox1.Controls)
            {
                if (c.GetType().Name == "CheckBox")
                {
                    ((CheckBox)c).Checked = true ;
                }
            }
        }

        private void btnSelectAll2_Click(object sender, EventArgs e)
        {
            foreach (Control c in groupBox2.Controls)
            {
                if (c.GetType().Name == "CheckBox")
                {
                    ((CheckBox)c).Checked = true;
                }
            }
        }

        private void btnSelectAll3_Click(object sender, EventArgs e)
        {
            foreach (Control c in groupBox3.Controls)
            {
                if (c.GetType().Name == "CheckBox")
                {
                    ((CheckBox)c).Checked = true;
                }
            }
        }

        private void btnSelectAll4_Click(object sender, EventArgs e)
        {
            foreach (Control c in groupBox4.Controls)
            {
                if (c.GetType().Name == "CheckBox")
                {
                    ((CheckBox)c).Checked = true;
                }
            }
        }

        private void btnSelectAll5_Click(object sender, EventArgs e)
        {
            foreach (Control c in groupBox5.Controls)
            {
                if (c.GetType().Name == "CheckBox")
                {
                    ((CheckBox)c).Checked = true;
                }
            }
        }
    }
}